#include "CGameScene.h"
#include <mutex>

CGameScene* CGameScene::instance = NULL;

CGameScene::CGameScene() : loginView(NULL), gameView(NULL), currentView(NULL)
{
	
}

CGameScene::~CGameScene()
{
	CC_SAFE_RELEASE_NULL(loginView);
	CC_SAFE_RELEASE_NULL(gameView);
}

CGameScene& CGameScene::getInstance()
{
	if (instance == NULL)
	{
		static std::mutex mtx;
		mtx.lock();
		if (instance == NULL)
		{
			instance = new CGameScene();
			instance->init();
		}
		mtx.unlock();
	}
	return *instance;
}

bool CGameScene::init()
{
	if (!CCScene::init())
	{
		return false;
	}
	changeView(getLoginView());
	return true;
}

CLoginView* CGameScene::getLoginView()
{
	if (loginView == NULL)
	{
		loginView = CLoginView::create();
		loginView->retain();
	}
	return loginView;
}

CGameView* CGameScene::getGameView()
{
	if (gameView == NULL)
	{
		gameView = CGameView::create();
		gameView->retain();
	}
	return gameView;
}

void CGameScene::changeView(CCLayer* view)
{
	if (currentView == view || view == NULL || view->getParent() != NULL)
	{
		return;
	}
	if (currentView)
	{
		removeChild(currentView);
	}
	currentView = view;
	addChild(view);
}

